package cn.com.huzo.webapp.controller.manufacture;

import cn.com.huzo.model.entities.manufacture.RequisitionSlipHeaderEntity;
import cn.com.huzo.model.entities.manufacture.RequisitionSlipLinesEntity;
import cn.com.huzo.service.manufacture.RequisitionSlipHeaderManager;
import cn.com.huzo.service.manufacture.RequisitionSlipLinesManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 领料单画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/manufacture/requisition_slip")
public class RequisitionSlipController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "RequisitionSlipHeaderManager")
    private RequisitionSlipHeaderManager _manager_header;

    @Resource(name = "RequisitionSlipLinesManager")
    private RequisitionSlipLinesManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到领料单一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("manufacture.requisition_slip.list.def");
        return view;
    }

    /**
     * 跳转领料单编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作类别 1：增加 2：编辑
     * @param slipId 领料单ID
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{slipId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "slipId") Long slipId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("headerId", slipId);
        view.setViewName("manufacture.requisition_slip.edit.def");
        return view;
    }

    /**
     * 领料单选择画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/choice.html")
    public ModelAndView redirectChoice(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.manufacture.requisition_slip.choice.def");
        return view;
    }

    /**
     * 弹出导出excel颜色选择画面
     * <p>
     * @return
     */
    @RequestMapping("/export_option.html")
    public ModelAndView redirectMdpExportExcelPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.manufacture.export_option.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        rtnVal.put("BillNo", _manager_header.fetchNewBillNo());
        return rtnVal;
    }

    /**
     * 根据标识取得领料单数据。
     *
     * @param criterion 领料单标识、当前用户、流程标识
     * @return 领料单数据（包括主表和子表）。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByHeaderKey(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId"));
    }

    /**
     * 根据条件取得领料单信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 领料单信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 新增领料单信息数据。
     * <p>
     * @param instance 要新增的领料单信息对象。
     * @return 新增记录主键、新增明细
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> insert(@RequestBody final RequisitionSlipHeaderEntity instance) throws Exception {
        return _manager_header.save(instance);
    }

    /**
     * 保存领料单信息数据。
     * <p>
     * @param instance 要保存的领料单信息对象。
     * @return 影响行数
     * @throws Exception
     */
    @RequestMapping(value = "/header/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer update(@RequestBody final RequisitionSlipHeaderEntity instance) throws Exception {
        return _manager_header.update(instance);
    }

    /**
     * 删除领料单信息。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }

    /**
     * 审核数据。
     * <p>
     * @param instance 要审核对象。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "/check.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer check(@RequestBody RequisitionSlipHeaderEntity instance) throws Exception {
        return null;   ///_manager_header.doCheck(instance);

    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="领料单明细数据处理部分">
    /**
     * 根据条件取得明细数据。
     * <p>
     * @param headerId 出库标识
     * @return 领料单明细
     * @throws Exception
     */
    @RequestMapping(value = "/lines/fetchLinesByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<RequisitionSlipLinesEntity> fetchLinesByHeaderKey(@RequestBody final Long headerId) throws Exception {
        return _manager_lines.fetchLinesByHeaderId(headerId);
    }

    /**
     * 补充（新增） 领料单明细数据（调达表明细发生增加的情况，领料单对应的增加）。
     *
     * @param criterion 要补充的数据
     * @return 领料单明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/supplement.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<RequisitionSlipLinesEntity> supplement(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.supplement(MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "ProductId"),
                MapUtils.getLong(criterion, "UserId"));
    }

    /**
     * 更新领料单明细
     * <p>
     * @param itemList 要更新的数据
     * @return 更新的记录数.
     * @throws Exception
     */
    @RequestMapping(value = "/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchUpdate(@RequestBody final List<RequisitionSlipLinesEntity> itemList) throws Exception {
        return _manager_lines.batchUpdate(itemList);
    }

    /**
     * 删除领料单明细数据。
     *
     * @param criterion 要删除的领料单数据
     * @return 领料单明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);

    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="导出excel">
    /**
     * 导出提前组料单excel文件
     * <p>
     * @param request 客户端请求
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "manufacture/requisitionSlip/export.html",
            method = RequestMethod.POST)
    public ModelAndView exportViewExcel(HttpServletRequest request) throws Exception {
        Long headerId = Long.parseLong(request.getParameter("_PrintHeaderId").trim());
        Long colourId = Long.parseLong(request.getParameter("_PrintColourId").trim());
        Long productId = Long.parseLong(request.getParameter("_PrintProductId").trim());
        Map<String, Object> condition = new HashMap();
        condition.put("HeaderKey", headerId);
        condition.put("ColourId", colourId);
        condition.put("ProductId", productId);
        Map<String, Object> model = null;  //_manager_header.fetchReportData(condition);   /////

        return new ModelAndView("RequisitionSlipExcelView", model);
    }

    /**
     * 根据给定的产品ID，取得生产指示书中分配的成品颜色数据。
     *
     * @param productId 产品标识
     * @return 生产指示中分配成品颜色数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "manufacture/requisitionSlip/fetchProductColour.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<Map<String, Object>> fetchProductColour(@RequestBody final Long productId) throws Exception {
        Map<String, Object> condition = new HashMap();
        condition.put("ProductId", productId);
        return null;    //_manager_header.fetchProductColour(condition);      ////////
    }
    // </editor-fold>
}
